package com.hefei.edu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hefei.edu.entity.Subject;
import com.hefei.edu.excel.SubjectExcel;
import com.hefei.edu.mapper.SubjectMapper;
import org.springframework.util.StringUtils;

/**
 * @author caizhengjun
 * @date 2021/3/5 下午7:39
 */
public class SubjectExcelListener extends AnalysisEventListener<SubjectExcel> {

    private SubjectMapper subjectMapper;

    public SubjectExcelListener() {
    }

    public SubjectExcelListener(SubjectMapper subjectMapper) {
        this.subjectMapper = subjectMapper;
    }

    @Override
    public void invoke(SubjectExcel row, AnalysisContext context) {

        System.out.println("context = " + context);

        System.out.println("正在读取excel文件");
        System.out.println("row = " + row);
    }

    private Subject subOtherExists(String title) {
        // 判断一级分类是否存在
        QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title", title);
        return this.subjectMapper.selectOne(queryWrapper);
    }

    private Subject sub1Exists(String title) {

        // 判断一级分类是否存在
        QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title", title)
                .eq("parent_id", 0);
        return this.subjectMapper.selectOne(queryWrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

        System.out.println("excel文件读取结束");
    }
}
